/**游戏循环
 * @date   2011-4-9
 * @author tianye
 */
package cn.edu.dlut.WingGame.Game;

import android.os.Handler;
import android.os.Message;
import cn.edu.dlut.WingGame.Tools.TimeCounter;

public class WingLoop extends Thread {
	public static final int MSG_UPDATE = 1;
	private TimeCounter timer = new TimeCounter();
	private boolean isRun = false;

	public WingLoop() {
		start();
	}

	public void setFPS(int fps) {
		timer.setInterval(1000.0f / fps);
		timer.restartTimer();
	}

	@Override
	public void run() {
		super.run();
		while (!Thread.interrupted() && isRun) {
			if (timer.checkTimer()) {
				WingGame.update();
				timer.restartTimer();
			} else {
				try {
					sleep(1);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			// try {
			//			
			// Message msg = new Message();
			// msg.what = MSG_UPDATE;
			// handler.sendMessage(msg);
			// Thread.sleep(100);
			// } catch (InterruptedException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }

		}

	}

	public void startLoop() {
		isRun = true;
	}

	public void stopLoop() {
		isRun = false;
	}

	public Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_UPDATE:

				break;
			}
		}
	};
}
